<!--
@Description:新增编辑
@Author:zqy
@Date:2024-09-24 14:08:22
@Last Modified by:zqy
@Last Modified time:2024-09-24 14:08:22
-->
<template>
  <el-dialog
    ref="formDlg"
    :title="`${!!(detail && detail.id)?'编辑':'新增'}`"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="500px"
    @close="closeDialog"
  >
    <div style="padding-right:30px">
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户:" prop="account">
              <input-with-dialog
                v-model="userData"
                :props="{label:'userName',value:'account'}"
                :focus-query="true"
                :multiple="false"
                placeholder="请输入姓名或账号进行查询"
                :config="{
                  title:'选择用户',
                  interfacePath:userApi,
                  method:'post',
                  setting:[
                    {prop:'userName',title:'用户姓名',width:120},
                    {prop:'account',title:'用户名',width:120},
                    {prop:'staffCode',title:'员工编码',width:120},
                    {prop:'phone',title:'电话',width:120}
                  ],
                  apiHeader: {},
                  paramsRaw:{},
                  paramField:'userName',
                  queryPosition:'filtersRaw',
                  queryCodekey:'account'
                }"
                @change="handleUserChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号:">
              {{ formData.account }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工编号:">
              {{ formData.staffCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话:">
              {{ formData.phone }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="组织:" prop="customerCode">
              <QupSearchSelectPicker
                :url="getCustomerApi"
                placeholder="请选择组织"
                lable-name="customerName"
                value-name="customerCode"
                custom-name="name"
                method="get"
                :value.sync="formData.customerCode"
                :lable.sync="formData.customerName"
                :params="{query:{},type:1}"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button v-loading="submiting" size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveConfig } from '@/api/accountCustomerConfig'
export default {
  name: 'AccountCustomerConfigEdit',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detail: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      userData: [],
      userApi: `${process.env.VUE_APP_BASE_API}/api/v1/user/page`,
      getCustomerApi: `${process.env.VUE_APP_BASE_API}/api/v1/accountCustomerConfig/customers`,
      formData: {
        account: '',
        userName: '',
        staffCode: '',
        phone: '',
        customerCode: '',
        customerName: ''
      },
      formRules: {
        account: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ],
        customerCode: [
          { required: true, message: '请选择经销商', trigger: 'change' }
        ]
      },
      submiting: false
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    if (this.detail && this.detail.id) {
      const { account, userName } = this.detail
      const keys = Object.keys(this.formData)
      keys.forEach(k => {
        this.$set(this.formData, k, this.detail[k] || '')
      })
      if (account && userName) {
        this.userData = [
          { label: userName, value: account }
        ]
      }
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      const keys = Object.keys(this.formData)
      keys.forEach(k => {
        this.$set(this.formData, k, '')
      })
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    handleUserChange(data) {
      const d = data && data[0] || {}
      this.$set(this.formData, 'account', d.account || '')
      this.$set(this.formData, 'userName', d.userName || '')
      this.$set(this.formData, 'staffCode', d.staffCode || '')
      this.$set(this.formData, 'phone', d.phone || '')
    },
    submitForm(type) {
      if (this.submiting) return
      this.$refs['refForm'].validate(async(valid) => {
        if (valid) {
          const data = Object.assign(this.formData)
          if (this.detail && this.detail.id) {
            data.id = this.detail.id
          }
          this.submiting = true
          await saveConfig(data).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                // message: res.msg ? res.msg : `${formData.id ? '编辑' : '新增'}保存成功`,
                message: `${data.id ? '编辑' : '新增'}保存成功`,
                type: 'success'
              })
              this.$emit('success', !data.id)
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
